
import {ResourceType} from '../libs/enum'
import {Resource, Searcher, Uploader} from '../api/index'
import store from '../store'
class Base{
    
    constructor(type){
        this.type = type
    }

    async get(id){
        return await Resource.get(this.type, id)
    }
    async gets(offset=0, limit=100){
        return await Resource.gets(this.type, offset, limit)
    }
    async upload(data){
        // 需要提供 setting 参数
        if(store.state.user.setting.step == 0){ // 应对后端API的规范
            store.state.user.setting.step == 'fps'
        }
        data = {...store.state.user.setting,...data}
        console.log(`实际上传参数： ${JSON.stringify(data)}`)
        return await Uploader.resource(this.type, data)
    }
    async del(id){
        return await Resource.del(this.type, id)
    }
    async search(id, key){
        return await Searcher.search(this.type, id, key)
    }
}


class Video extends Base{
    constructor(){
        super(ResourceType.VIDEO)
    }
    // async getByUrl(url){
    //     return (await MyHttp.post(`/resource/video/get/by/url`, {url})).data
    // }

    async upload(file){
        return await super.upload({file})
    }

    async getCatalog(id){
        return (await Resource.getCatalog(id))
    }
}

class Chapter extends Base{
    constructor(){
        super(ResourceType.CHAPTER)
    }
    async upload(dir_path){
        return await super.upload({path: dir_path})
    }
}

class Course extends Base{
    constructor(){
        super(ResourceType.COURSE)
    }
    async upload(dir_path){
        return await super.upload({path: dir_path})
    }
}

export {
    Video,
    Chapter,
    Course
}